﻿using System;
using PhoneCore.Framework;
using PhoneCore.Framework.Bootstrap;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.Diagnostic.Tracing;
using PhoneCore.Framework.IoC;
using PhoneCore.Framework.Storage;
using SecureBox.Tools;

namespace SecureBox.UI.Infrastructure.Plugins
{
    /// <summary>
    /// Initialize password generator settings
    /// </summary>
    public class PassGenPlugin: BootstrapperPlugin
    {
        public PassGenPlugin(IConfigSection config) : base(config) { }

        public override bool Run()
        {
            Container.RegisterType<IPasswordGenerator, PasswordGenerator>();
            return Initialize();
        }

        public override bool Update()
        {
            return Initialize();
        }

        private bool Initialize()
        {
            //get generator
            IPasswordGenerator generator = Container.Resolve<IPasswordGenerator>();

            //configure generator using settings service
            ISettingService settingsService = Container.Resolve<ISettingService>();

            //initialize minimum
            if (!settingsService.IsExist(Constants.PassGenMinimumSettingName))
                settingsService.Save(Constants.PassGenMinimumSettingName, Constants.PassGenMinimumDefault);
            generator.Minimum = settingsService.Load<int>(Constants.PassGenMinimumSettingName);
            //initialize maximum
            if (!settingsService.IsExist(Constants.PassGenMaximumSettingName))
                settingsService.Save(Constants.PassGenMaximumSettingName, Constants.PassGenMaximumDefault);
            generator.Maximum = settingsService.Load<int>(Constants.PassGenMaximumSettingName);

            generator.ExcludeSymbols = true;
            generator.Exclusions = "`~!@#$%^&*()-_=+[]{}\\|;:'\",<.>/?";

            return false;
        }
    }
}
